package org.pr.logoupdate.servlet;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.pr.logoupdate.util.Constant;
import org.pr.logoupdate.util.ImageUtil;
import org.pr.logoupdate.util.StringUtil;

/**
 * Servlet implementation class LogoIconMergeServlet
 */
@WebServlet("/servlet/iconMerge")
public class LogoIconMergeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static Logger logger = Logger.getLogger(LogoIconMergeServlet.class);
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LogoIconMergeServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
//		response.getWriter().append("Served at: ").append(request.getContextPath());
		String filename = (String)request.getSession().getAttribute(Constant.UPLOAD_FILENAME);
		
		if(filename == null) {
			//no logo uploaded
			request.getSession().setAttribute(Constant.PAGE1_ERROR, "加Icon时似乎头像不存在或者超时了");
			response.sendRedirect(request.getContextPath() + "/index.jsp");
			return;
		}
		String tempFolder = System.getProperty("java.io.tmpdir");
		String sessionId = request.getSession().getId();
		File imgFile = new File(tempFolder + File.separator + sessionId
				+ File.separator + filename);
		if (!imgFile.exists() || imgFile.isDirectory() || !imgFile.canRead() || !imgFile.canWrite()) {
			request.getSession().setAttribute(Constant.PAGE1_ERROR, "系统内部错误，请重新上传");
			response.sendRedirect(request.getContextPath() + "/index.jsp");
			return;
		}
		String iconSrc = request.getParameter("iconSrc");
		String logoInfo = request.getParameter("logoInfo"); //w,h
		String iconInfo = request.getParameter("iconInfo");//left,top,w,h
		logger.debug("iconSrc=" + iconSrc + ",logo="+ logoInfo
				+ ",icon=" + iconInfo);
		InputStream iconStream = request.getServletContext().getResourceAsStream("/" + iconSrc);
		String iconTemppath = ImageUtil.saveInputStream(iconStream, 
				tempFolder + File.separator + sessionId, StringUtil.getFileSuffix(iconSrc));
		iconStream.close();
		int logoW = Double.valueOf(logoInfo.split(",")[0]).intValue();
		int logoH = Double.valueOf(logoInfo.split(",")[1]).intValue();
		int iconLeft = Double.valueOf(iconInfo.split(",")[0]).intValue();
		int iconTop = Double.valueOf(iconInfo.split(",")[1]).intValue();
		int iconW = Double.valueOf(iconInfo.split(",")[2]).intValue();
		int iconH = Double.valueOf(iconInfo.split(",")[3]).intValue();
		ImageUtil.logoIconMerge(imgFile.getAbsolutePath(), iconTemppath, 
				logoW, logoH, iconLeft, iconTop, iconW, iconH);
		response.sendRedirect(request.getContextPath() + "/step4.jsp");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
